<template>
  <div>
    <el-form :model="queryParams" ref="queryForm" v-show="showSearch" size="small" :inline="true"
             label-width="55px" style="background-color: #f5f5f5;padding-top: 15px">
      <el-form-item label="姓名" prop="name">
        <el-input
          style="width: 170px"
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="身份证" prop="cardNo">
        <el-input
          style="width: 180px"
          v-model="queryParams.cardNo"
          placeholder="请输入身份证"
          clearable
          @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="电话" prop="tel">
        <el-input
          style="width: 170px"
          v-model="queryParams.tel"
          placeholder="请输入电话"
          clearable
          @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="地址" prop="location">
        <el-input
          style="width: 200px"
          v-model="queryParams.location"
          placeholder="请输入地址"
          clearable
          @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row style="padding: 5px 16px">
      <el-col :span="2">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:blacklist:add']">新增
        </el-button>
      </el-col>
      <el-col :span="2">
        <el-button
          type="danger"
          size="mini"
          @click="weihu()"
          v-hasPermi="['system:blacklist:remove']">维护风险标签
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"
                     :columns="columns" title="展示更多关键字" v-hasRole="['admin']"></right-toolbar>
    </el-row>

    <el-table v-show="total>0" v-loading="loading" :data="blacklistList">
      <el-table-column label="ID" align="center" prop="id"/>
      <el-table-column label="姓名" align="center" prop="name"/>
      <el-table-column label="身份证" align="center" prop="cardNo"/>
      <el-table-column label="电话" align="center" prop="tel"/>
      <el-table-column label="地址" align="center" prop="location" v-if="columns[0].visible"/>
      <el-table-column label="风险关键字" align="center">
        <template slot-scope="scope">
          <div style="display: flex; justify-content: center; ">
            <div v-for="item in JSON.parse(scope.row.labels)"
                 style="margin: 1px;padding:3px 6px;background-color: red;
               border-radius: 3px;color: #fff;">
              {{ item }}
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updateTime" v-if="columns[1].visible"/>
      <el-table-column label="操作" align="center" width="180" v-hasRole="['admin']">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:blacklist:edit']">修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:blacklist:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"/>
    <!-- 添加或修改黑名单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="60%" :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="name" ref="name">
          <el-input v-model.trim="form.name" placeholder="请输入姓名"/>
        </el-form-item>
        <el-form-item label="身份证" prop="cardNo" ref="cardNo">
          <el-input v-model.trim="form.cardNo" placeholder="请输入身份证"/>
        </el-form-item>
        <el-form-item label="电话" prop="tel" ref="tel">
          <el-input v-model.trim="form.tel" placeholder="请输入电话"/>
        </el-form-item>
        <el-form-item label="地址" prop="location">
          <el-input v-model.trim="form.location" placeholder="请输入地址关键字"/>
        </el-form-item>
        <el-form-item label="风险标签" prop="label" ref="labels">
          <el-select style="width: 100%;" v-model="form.labels" multiple
                     filterable allow-create placeholder="请选择风险标签">
            <el-option
              v-for="item in labelsOptions"
              :label="item"
              :value="item">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model.trim="form.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="增加/移除风险标签" :visible.sync="open_" width="70%" :close-on-click-modal="false">
      <el-select style="width: 100%;"
                 v-model="labels"
                 multiple
                 filterable
                 allow-create
                 placeholder="请输入选择风险标签">
      </el-select>
      <div slot="footer">
        <el-button type="primary" @click="updateLabels">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  import {
    listBlacklist,
    getBlacklist,
    delBlacklist,
    addBlacklist,
    updateBlacklist,
    labels,
    setLabels
  } from "@/api/system/blacklist";

  export default {
    name: "Blacklist",
    data() {
      let validateName = (rule, value, callback) => {
        if (!this.form.name && !this.form.cardNo && !this.form.tel) {
          callback(new Error("姓名，身份证，电话请至少填写一项"))
        } else {
          //任意值被填写，清除验证提示
          if (!this.form.name || !this.form.cardNo || !this.form.tel) {
            this.$refs.name.clearValidate()
            this.$refs.cardNo.clearValidate()
            this.$refs.tel.clearValidate()
          }
          callback();
        }
      }
      return {
        // 遮罩层
        loading: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 黑名单表格数据
        blacklistList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        open_: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          name: null,
          cardNo: null,
          tel: null,
          location: null
        },
        columns: [
          {key: 0, label: `地址`, visible: false},
          {key: 1, label: `更新时间`, visible: false}
        ],
        labelsOptions: [],
        labels: [],
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          name: [
            {
              required: true,
              validator: validateName,
              trigger: "blur",
            },
          ],
          cardNo: [
            {
              required: true,
              validator: validateName,
              trigger: "blur",
            },
          ],
          tel: [
            {
              required: true,
              validator: validateName,
              trigger: "blur",
            },
          ],
          labels: [
            {required: true, message: "风险标签不能为空", trigger: "blur"}
          ]
        }
      };
    },
    created() {
      if (this.$auth.hasRole('admin')) {
        this.getList();
      } else {
        this.loading = false;
      }
    },
    methods: {
      updateLabels() {
        console.log('-----------');
        console.log(this.labels);
        setLabels(this.labels).then(response => {
          if (response.code == 200) {
            this.$modal.msgSuccess("修改成功");
          }
        });
      },
      /** 查询黑名单列表 */
      getList() {
        this.loading = true;
        listBlacklist(this.queryParams).then(response => {
          this.blacklistList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          name: null,
          cardNo: null,
          tel: null,
          location: null,
          labels: [],
          remark: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        if (this.queryParams.name || this.queryParams.cardNo || this.queryParams.tel || this.queryParams.location) {
          this.queryParams.pageNum = 1;
          this.getList();
        }
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        labels().then(res => {
          this.labelsOptions = res.data;
          this.title = "添加行业黑名单";
          this.open = true;
        })
      },
      weihu() {
        labels().then(res => {
          this.labels = res.data;
          this.open_ = true;
        })
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id
        labels().then(res => {
          this.labelsOptions = res.data;
        })
        getBlacklist(id).then(response => {
          this.form = response.data;
          this.form.labels = JSON.parse(this.form.labels);
          this.title = "修改黑名单";
          this.open = true;
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateBlacklist({
                ...this.form,
                labels: JSON.stringify(this.form.labels)
              }).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addBlacklist({
                ...this.form,
                labels: JSON.stringify(this.form.labels)
              }).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        this.$modal.confirm('是否确认删除黑名单编号为"' + row.id + '"的数据项？').then(function () {
          return delBlacklist(row.id);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      }
    }
  };
</script>
<style scoped>
  ::v-deep .my_dialog .el-dialog__body {
    padding: 12px 22px;
  }
</style>
